/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-10-10 16:25:31
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-11-11 08:30:34
 * @FilePath: \实训二——宏业找房\egg\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const { Controller } = require('egg');
class HomeController extends Controller {
  // 后端获取房源数据
  async housing() {
    const { ctx } = this;
    const results = await this.app.mysql.select('housing');
    const { one, two, three, four, pageCode, pageSize, } = ctx.request.body;
    let newLists = results;
    if (one) {
      newLists = newLists.filter(v => v.address.includes(one))
    }
    if (two) {
      newLists = newLists.filter(v => v.address.includes(two))
    }
    if (three) {
      newLists = newLists.filter(v => v.address.includes(three))
    }
    if (four) {
      newLists = newLists.filter(v => v.name.includes(four))
    }

    let total = newLists.length;
    newLists = newLists.slice((pageCode * 1 - 1) * pageSize, pageCode * pageSize)

    if (results) {
      ctx.body = {
        code: 200,
        list: newLists,
        total,
        msg: '获取成功',
        results,
        // lists,//前端客户端
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '获取失败'
      }
    }
  }
  // 前端获取房源数据
  async housings() {
    const { ctx } = this;
    const results = await this.app.mysql.select('housing');
    const { value, pickValue, roomType, oriented, floor, characteristic } = ctx.request.body;
    let lists = results;
    // 客户端
    if (value === "东" || value === '西' || value === "南" || value === '北') {
      lists = lists.filter(v => v.direction === value)
    }
    if (value === "new" || value === "old") {
      lists = lists.filter(v => v.isNew === value)
    }

    if (value == '10') {
      lists = lists.filter(v => (v.price * v.area) < (value * 10000))
    }
    if (value == '80') {
      lists = lists.filter(v => (v.price * v.area) > (value * 10000))
    }

    // 前端区域选择
    if (pickValue[0]) {
      lists = lists.filter(v => v.address.includes(pickValue[0]))
    }
    if (pickValue[1]) {
      lists = lists.filter(v => v.address.includes(pickValue[1]))
    }
    if (pickValue[2]) {
      lists = lists.filter(v => v.address.includes(pickValue[2]))
    }

    //户型筛选
    if (roomType.length > 0) {
      let roomArr = []
      results.map((item) => {
        roomType.map(v => {
          if (item.areas == v) {
            roomArr.push(item)
          }
        })
      })
      lists = roomArr
    }

    // 朝向筛选
    if (oriented.length > 0) {
      let newArr = []
      results.map((item) => {
        oriented.map(v => {
          if (item.direction == v) {
            newArr.push(item)
          }
        })
      })
      lists = newArr
    }

    // 楼层筛选
    if (floor.length > 0) {
      let newArr = []
      results.map((item) => {
        floor.map(v => {
          if (item.floor == v) {
            newArr.push(item)
          }
        })
      })
      lists = newArr
    }

    //房屋亮点筛选
    if (characteristic.length > 0) {
      let newArr = []
      results.map((item) => {
       JSON.parse(item.page).map(v => {
          characteristic.map(i => {
            if (v == i) {
              newArr.push(item)
            }
          })
        })
      })
      lists = newArr
    }
    if (results) {
      ctx.body = {
        code: 200,
        msg: '获取成功',
        results,
        lists,//前端客户端
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '获取失败'
      }
    }
  }
  // 经纪人获取新房源数据
  async agentNewHousing() {
    const { ctx } = this;
    const results = await this.app.mysql.select('agentnewhousing');
    if (results) {
      ctx.body = {
        code: 200,
        msg: '获取成功',
        total:results.length
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '获取失败'
      }
    }
  }
  // 经纪人获取二手房源数据
  async agentOldHousing() {
    const { ctx } = this;
    const results = await this.app.mysql.select('agentoldhousing');
    if (results) {
      ctx.body = {
        code: 200,
        msg: '获取成功',
        total: results.length
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '获取失败'
      }
    }
  }
  
  // 详情数据
  async housingDetail() {
    const { ctx } = this;
    const { id } = ctx.request.body;
    const results = await this.app.mysql.get('housing', { id });
    if (results) {
      ctx.body = {
        code: 200,
        msg: '详情数据成功',
        list: results
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '详情数据失败',
      }
    }
  }
  // 删除数据
  async del() {
    const { ctx } = this;
    const { id } = ctx.query;
    const result = await this.app.mysql.delete('housing', { id });
    if (result) {
      const results = await this.app.mysql.select('housing');
      ctx.body = {
        code: 200,
        msg: '删除成功',
        list: results
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '删除失败'
      }
    }

  }

  // 添加数据
  async add() {
    const { ctx } = this;
    const { name, address, price, status, manager } = ctx.request.body;
    const result = await this.app.mysql.insert('housing', { name, address, price, sort: 1, updateTime: new Date().toLocaleString('Chinese', { hour12: false }), status, manager });
    if (result) {
      const results = await this.app.mysql.select('housing');
      ctx.body = {
        code: 200,
        msg: '添加成功',
        list: results
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '添加失败'
      }
    }
  }
  // 编辑
  async edit() {
    const { ctx } = this;
    const { editId, name, address, price, status, manager } = ctx.request.body;
    const row = {
      name,
      address,
      price,
      updateTime: new Date().toLocaleString('Chinese', { hour12: false }),
      status,
      manager
    };

    const options = {
      where: {
        id: editId
      }
    };
    const result = await this.app.mysql.update('housing', row, options);
    if (result) {
      ctx.body = {
        code: 200,
        msg: '修改成功',
        list: result
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '修改失败',
      }
    }
  }
  // 获取经纪人名单
  async agent() {
    const { ctx } = this;
    const results = await this.app.mysql.select('agent');
    let newLists = results;

    newLists = newLists.filter(v => v.status === '在职')
    if (results) {
      ctx.body = {
        code: 200,
        msg: '经纪人名单',
        list: newLists
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '经纪人名单错误'
      }
    }
  }
  // 指派经纪人
  async agents() {
    const { ctx } = this;
    const { agents, id } = ctx.request.body;
    const row = {
      id,
      // ...ctx.request.body
      manager: agents,
    };


    const result = await this.app.mysql.update('housing', row,);
    if (result) {
      ctx.body = {
        code: 200,
        msg: '指派成功',
        list: result
      }
    } else {
      ctx.body = {
        code: 500,
        msg: '指派失败',
      }
    }
  }
  // 状态的改变
  async changeHousingStatus() {
    const { ctx } = this;
    const { id, status } = ctx.request.body;
    // 关闭
    if (status === 'true') {
      const row = {
        status: 'false'
      };

      const options = {
        where: {
          id
        }
      };
      const result = await this.app.mysql.update('housing', row, options);
      if (result) {
        ctx.body = {
          code: 200,
          msg: '关闭成功',
        }
      } else {
        ctx.body = {
          code: 500,
          msg: '关闭失败'
        }
      }
    } else if (status === 'false') {
      // 开启
      const row = {
        status: 'true'
      };

      const options = {
        where: {
          id
        }
      };
      const result = await this.app.mysql.update('housing', row, options);
      if (result) {
        ctx.body = {
          code: 200,
          msg: '开启成功',
        }
      } else {
        ctx.body = {
          code: 500,
          msg: '开启失败'
        }
      }
    }
  }

}

module.exports = HomeController;
